package org.agh.air.bpel2bpmn;

import com.sun.org.apache.xerces.internal.parsers.DOMParser;
import java.awt.Dimension;
import java.io.IOException;
import java.io.InputStream;
import org.apache.log4j.Logger;
import javax.swing.Box;
import javax.swing.DropMode;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import org.agh.air.bpel2bpmn.exception.ImproperInputFileException;
import org.agh.air.bpel2bpmn.walker.XmlWalker;
import org.agh.air.bpel2bpmn.util.Util;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class App {

    private static final Logger logger = Logger.getLogger(Class.class);
    public static String text;
    public static JList list = new JList();
    public static JTextArea inputArea = new JTextArea();
    public static JTextArea outputArea = new JTextArea();

    public static void main(String args[]) throws SAXException, IOException {


        JFrame frame = new JFrame("Bpel2Bpmn");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        inputArea.setLineWrap(true);
        outputArea.setLineWrap(true);
        inputArea.setEditable(false);
        inputArea.setDragEnabled(true);
        inputArea.setDropMode(DropMode.INSERT);
        inputArea.setTransferHandler(new Bpel2BpmnTransferHandler());
        JScrollPane spIn = new JScrollPane(inputArea);
        JScrollPane spOut = new JScrollPane(outputArea);
        spIn.setPreferredSize(new Dimension(620, 600));
        spOut.setPreferredSize(new Dimension(620, 600));
        Box bh = Box.createHorizontalBox();
        bh.add(spIn);
        bh.add(spOut);
        frame.add(bh);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);


    }

    public static void start(String bpelInput) throws IOException {

        DOMParser parser = new DOMParser();
        InputStream is;
        bpelInput = Util.removeWhiteCharsFromInputStream(bpelInput);
        is = Util.stringToStream(bpelInput);

        InputSource isource = new InputSource(is);
        try{
            parser.parse(isource);

            Document doc = parser.getDocument();
            Node ele = doc.getFirstChild();

            XmlWalker xmlWalker = new XmlWalker();
        
            String str = xmlWalker.walk(ele, doc);
            logger.info(Util.specialCharReplacer(str));
            outputArea.setText(Util.specialCharReplacer(str));
        } catch (ImproperInputFileException ex) {
            logger.error("", ex);
            JOptionPane.showMessageDialog(null, "Niepoprawny plik.");
        } catch (SAXException ex) {
            logger.error("", ex);
            JOptionPane.showMessageDialog(null, "Niepoprawnie sformatowany plik.");

        }
        
    }
}
